<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script>
        window.onload = function () {
            var canvas = document.getElementById('canvas'),
                context = canvas.getContext('2d'),
                points = [],
                numPoints = 9,
                ctrlPoint = {}

            for (var i = 0; i < numPoints; i++) {
                points.push({
                    x: Math.random() * canvas.width,
                    y: Math.random() * canvas.height
                })
            }
            console.log(points)

            context.beginPath()
            context.moveTo(points[0].x, points[1].y)

            for (i = 1; i < numPoints - 2; i++) {
                ctrlPoint.x = (points[i].x + points[i + 1].x) / 2
                ctrlPoint.y = (points[i].y + points[i + 1].y) / 2
                context.quadraticCurveTo(points[i].x, points[i].y, ctrlPoint.x, ctrlPoint.y)
            }
            //最后两个点
            console.log(i)
            context.quadraticCurveTo(points[i].x, points[i].y, points[i + 1].x, points[i + 1].y)

            context.stroke()
        }
    </script>
</body>

</html>